
var ItemController = {

    selectDistrict:function(){
        var provinceId = document.getElementById('province').value;
        var strDistrict = document.getElementById('district_hidden').value;
        var districtId =   document.getElementById('district');
        var districtOpts = districtId.options.length;
        //remove district
        for(var n = districtOpts - 1; n > 0; n-- ){
            districtId.removeChild(districtId.options[n]);
        }
        
        var objDistrict = strDistrict.split('$');
        var districtLenght = objDistrict.length;
        //add new district option
        if(provinceId != '0'){
            for(var i=0; i < districtLenght; i++){
                var aryDistrict = objDistrict[i].split(';');
                if(aryDistrict[2] == provinceId){
                    var opt = document.createElement('OPTION');
                    opt.value = aryDistrict[0];
                    if(navigator.appName.indexOf('Microsoft') !=-1){
                        opt.innerText = aryDistrict[1];
                    }else{
                        opt.text = aryDistrict[1];
                    }
                    districtId.appendChild(opt);
                }
            }
            if(districtId.options.length <= 1){
                districtId.disabled = 'disabled';
            }else{
                districtId.disabled = '';
            }
        }else{
            districtId.disabled = 'disabled';
        }
        
    },

    selectCategories:function(){
        var needId = document.getElementById('needs').value;
        var strCategories = document.getElementById('categories_hidden').value;
        var catId =   document.getElementById('categories');
        var catOpts = catId.options.length;
        //remove district
        for(var n = catOpts - 1; n > 0; n-- ){
            catId.removeChild(catId.options[n]);
        }

        var objCategories = strCategories.split('$');
        var categoriesLenght = objCategories.length;
        //add new district option
        if(needId != '0'){
            for(var i=0; i < categoriesLenght; i++){
                var aryCategories = objCategories[i].split(';');
                if(aryCategories[2] == needId){
                    var opt = document.createElement('OPTION');
                    opt.value = aryCategories[0];
                    if(navigator.appName.indexOf('Microsoft') !=-1){
                        opt.innerText = aryCategories[1];
                    }else{
                        opt.text = aryCategories[1];
                    }
                    catId.appendChild(opt);
                }
            }
            if(catId.options.length <= 1){
                catId.disabled = 'disabled';
            }else{
                catId.disabled = '';
            }
        }else{
            catId.disabled = 'disabled';
        }
    },

    selectProject:function(needId){
//        var needId = document.getElementById('district').value;
//        alert(needId);
        var strCategories = document.getElementById('project_hidden').value;
        var catId =   document.getElementById('project');
        var catOpts = catId.options.length;
        //remove district
        for(var n = catOpts - 1; n > 0; n-- ){
            catId.removeChild(catId.options[n]);
        }

        var objCategories = strCategories.split('$');
        var categoriesLenght = objCategories.length;
        //add new district option
        if(needId != '0'){
            for(var i=0; i < categoriesLenght; i++){
                var aryCategories = objCategories[i].split(';');
                if(aryCategories[2] == needId){
                    var opt = document.createElement('OPTION');
                    opt.value = aryCategories[0];
                    if(navigator.appName.indexOf('Microsoft') !=-1){
                        opt.innerText = aryCategories[1];
                    }else{
                        opt.text = aryCategories[1];
                    }
                    catId.appendChild(opt);
                }
            }
            if(catId.options.length <= 1){
                catId.disabled = 'disabled';
            }else{
                catId.disabled = '';
            }
        }else{
            catId.disabled = 'disabled';
        }
    }
}